var windowName = "main";
var Page,browser,load = 0;

var winName;
var urls;
var frames = [];
apiready = function(){
    onWindowShow(function () {
        log("root i Show")
        getConfig()
        //获取版本信息存到本地
        var url = window.location.search;
        var version = url.substring(3);
        setLocData("version",version);
        log(version,"version:")

        //检查网络状态
        var cType = api.connectionType;
        log(cType,"网络状态")


        //主页重回前台时调用相应函数
        if (load > 0){
            updateFrameData(Page.index)
        }
        load++
    })
    onWindowHide(function () {
    })

    var cookie = getLocData("cookie");
    browser = api.require('webBrowser');
    Page = createVM("#Page",{
        //页面数据
        index:0,
        tab:"index",
        shift:'home',
        showSite:false
    },{
        //页面方法
        "init":function () {
            var rnd = "?t=1.01";
            winName = ["index","site","news","user"];
            urls = [
                pageUrl + "/ApiCloud/Page/Shop/index.html"+rnd,
                pageUrl + "/ApiCloud/Page/Shop/site.html"+rnd,
                // pageUrl + "/ApiCloud/Page/Shop/news.html"+rnd,
                pageUrl + "/ApiCloud/Page/Shop/user.html"+rnd
            ];
            logJson(urls)

            for (var i = 0,len = urls.length; i < len; i++) {
                frames.push( {
                    // name: 'frame'+i,
                    name:winName[i],
                    url: urls[i],
                    bgColor : 'rgba(0,0,0,.0)',
                    bounces:false
                } )
            }
            funIniGroup();



            getConfig();

        }
        ,"change":function (value) {
            log(value,"value:")
            var index = winName.indexOf(value);
            this.index = index;
            updateFrameData(index)
            api.setFrameGroupIndex({name: 'group', index:index });
        }
    })
    Page.init();
}

function funIniGroup(){
    api.openFrameGroup({
        name: 'group',
        scrollEnabled: true,
        rect: {
            x: 0,
            y: 0,
            w: api.winWidth,
            h: $api.dom('#main').offsetHeight
        },
        index: 0,
        frames: frames,
        preload:3
    }, function (ret, err) {
        var name =  ret.name;
        var index =  winName.indexOf(name)
        // Page.tab = "index" + (ret.index + 1)
        Page.tab = winName[index]
    });
}


function updateFrameData(index) {
    setTimeout(function () {
        var jsfun = 'updateWinData();';
        api.execScript({
            frameName: winName[index],
            script: jsfun
        });
    },1000);
}



function getConfig() {

    apiGet("/app/get/config",{},function (res) {
        setLocData("banner",res.data.banner);
        setLocData("news",{text:res.data.news});

        //用户登录状态
        if (res.data.login === 0){
            showToast("用户尚未登录")
            goto("login")
            return;
        }else {

        }

    })
}